﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using YouMa.Reposity;
using YouMa.Model;
using Microsoft.IdentityModel.Protocols;
using YouMa.Helper;

namespace YouMa.Controllers
{
    [Produces("application/json")]
    [Route("api/UserLog")]
    public class UserLogController : BaseController
    {
        private readonly UserReposity UserReposity;
        public UserLogController(UserReposity _UserReposity)
        {

            UserReposity = _UserReposity;
        }

        /// <summary>
        /// 用户名和手机号注册
        /// </summary>
        /// <param name="username"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpPost, Route("Register")]
        public IActionResult Register(string username, string phone)
        {
            bool IsExist = false;
            IsExist = UserReposity.Exists<User>(a => a.Phone == phone);
            if (IsExist)
            {
                return Ok(new { msg = "该手机号已被注册", isSuccess = false });
                //直接提示已被注册，发送验证码到该号码，成功登陆
            }
            var user = new User();
            user.UserName = username;
            user.Password = "123456";
            user.CreateTime = DateTime.Now;
            user.EditTime = DateTime.Now;

            //第三方账号注册weixin
            return Ok(new { msg = "注册成功", isSuccess = true });
        }

        /// <summary>
        /// 手机号注册
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpPost, Route("Register1")]
        public IActionResult Register1(string phone)
        {
            bool IsExist = false;
            IsExist = UserReposity.Exists<User>(a => a.Phone == phone);
            //调用短信接口

            //存在则由验证码登录，不存在则由验证码注册
            if (IsExist)
            {
                return Ok(new { msg = "该手机号已被注册，现由验证码直接登录", isSuccess = true });
            }
            else
            {
                return Ok(new { msg = "注册成功", isSuccess = false });
            }
        }

        /// <summary>
        /// 登录验证
        /// </summary>
        /// <param name="username"></param>
        /// <param name="passward"></param>
        /// <returns></returns>
        [HttpPost, Route("LoginVerify")]
        public IActionResult LoginVerify(string username, string passward)
        {
            var model = UserReposity.GetEntity<User>(a => (a.UserName == username) && a.Password == passward);
            if (model != null)
            {
                return Ok(new { msg = "登录成功", isSuccess = true });
            }
            return Ok(new { msg = "手机号或密码错误", isSuccess = false });
        }

        /// <summary>
        /// 手机号,密码登录
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost, Route("Login")]
        public IActionResult Login(string phone, string password)
        {
            bool IsExist = false;
            IsExist = UserReposity.Exists<User>(a => a.Phone == phone && a.Password == password);
            if (IsExist)
            {
                return Ok(new { msg = "登录成功", isSuccess = true });
            }
            else
            {
                return Ok(new { msg = "手机号或密码错误", isSuccess = false });
            }
        }

        /// <summary>
        /// 完善个人信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost, Route("PerfectInfo")]
        public IActionResult PerfectInfo(User user)
        {
            var model = UserReposity.GetEntity<User>(a => a.Phone == user.Phone);
            model.UserName = user.UserName;
            model.Remark = user.Remark;
            model.Age = user.Age;
            model.Photo = user.Photo;
            model.Sex = user.Sex;
            model.Brithday = user.Brithday;
            model.Address = user.Address;
            model.College = user.College;
            model.Job = user.Job;
            var IsUpdate = UserReposity.UpdateEntity<User>(model);
            if (IsUpdate)
            {
                return Ok(new { msg = "编辑成功", isSuccess = true });
            }
            else
            {
                return Ok(new { msg = "编辑失败", isSuccess = false });
            }
            //}
            //else
            //{
            //return View("该用户不存在");
            //}
        }

        #region 找回密码
        /// <summary>
        /// 重设密码发送短信验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpPost, Route("SendFindMessage")]
        public IActionResult SendFindMessage(string phone)
        {
            var model = UserReposity.GetEntity<User>(a => a.Phone == phone);
            if (model != null && model.Id > 0)
            {
                var SmsFindPwdUrl = ConfigurationManager.GetSection("SmsFindPwdUrl");
                //string timeStamp= Encryption.G
                var url = string.Format(SmsFindPwdUrl, phone);
                //var returnmsg = HttpHelper.Get<ReturnMsg>(url);
                return Ok(new { msg = "验证码发送成功", isSuccess = true });
            }
            else
            {
                return Ok(new { msg = "该手机号未被注册", isSuccess = false });
            }
        }

        /// <summary>
        /// 验证码正确与否判断
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost, Route("VerifyFindCode")]
        public IActionResult VerifyFindCode(string phone, string code)
        {
            try
            {
                var model = UserReposity.GetEntity<User>(a => a.Phone == phone);
                if (model != null && model.Id > 0)
                {
                    var SmsFindPwdUrl = ConfigurationManager.GetSection("SmsFindPwdUrl");
                    //string timeStamp= Encryption.G
                    var url = string.Format(SmsFindPwdUrl, phone, code);
                    //var returnmsg = HttpHelper.Get<ReturnMsg>(url);
                    return Ok(new { msg = "验证码正确", isSuccess = true });
                    //验证码输入错误
                }
                else
                {
                    return Ok(new { msg = "该手机号未注册", isSuccess = false });
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="newpsd"></param>
        /// <returns></returns>
        [HttpPost, Route("ResetPassword")]
        public IActionResult ResetPassword(string phone,string newpsd)
        {
            var model = UserReposity.GetEntity<User>(a => a.Phone ==phone);
            if (model != null && model.Id > 0)
            {
                model.Password = newpsd;
                model.EditTime = DateTime.Now;
                var b = UserReposity.UpdateEntity<User>(model);
                if (b)
                {
                    return Ok(new { msg = "修改密码成功，请重新登录", isSuccess = true });
                }
                return Ok(new { msg = "修改密码失败", isSuccess = false });
            }
            else
            {
                return Ok(new { msg = "该手机号未注册", isSuccess = false });
            }
        }

        #endregion

        
    }
}